package servicesImp.upms;

import base.IBaseMapperWithoutBLOBs;
import base.IBaseServicesWithoutBLOBsImpl;
import base.response.OffsetLimitPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import services.company.CompanyInfoServices;
import services.expertInfo.ExpertBaseServices;
import services.staff.StaffInfoServices;
import services.upms.AccountPermissionServices;
import services.upms.AccountServices;
import services.upms.PermissionRoleServices;
import services.upms.RoleServices;
import util.key.IDFactory;
import wt.dao.upms.AccountMapper;
import wt.entity.company.CompanyInfo;
import wt.entity.expertInfo.ExpertBase;
import wt.entity.staff.StaffInfo;
import wt.entity.upms.*;
import wt.staticData.ShortData;
import wt.staticData.TableName;

import java.util.*;


/**
 * Created by Administrator on 2017/9/7.
 */
@Service
public class AccountServicesImp extends IBaseServicesWithoutBLOBsImpl< Account, AccountExample> implements AccountServices {

    @Autowired
    AccountMapper accountMapper;
    @Autowired
    AccountPermissionServices accountPermissionServices;
    @Autowired
    RoleServices roleServices;
    @Autowired
    ExpertBaseServices expertBaseServices;
    @Autowired
    CompanyInfoServices companyInfoServices;
    @Autowired
    StaffInfoServices staffInfoServices;
    @Autowired
    PermissionRoleServices permissionRoleServices;

    @Override
    public IBaseMapperWithoutBLOBs<Account, AccountExample> getMapper() {
        return accountMapper;
    }

    @Override
    public String getTableName() {
        return TableName.UPMS_ACCOUNT;
    }

    @Transactional
    public void insertSelective(Account account, List<String> permissionIdList) throws Exception {
        if (account.getCreateTime()==null){
            account.setCreateTime(new Date());
        }
        if (account.getLive()==null){
            account.setLive(ShortData.s1);
        }

        getCurrentProxy().insertSelective(account);
        AccountPermission accountPermission = new AccountPermission();
        accountPermission.setAccountId(account.getId());
        accountPermission.setType((short) 1);
        for (String id : permissionIdList) {
            accountPermission.setId(IDFactory.getSnowflakeId());
            accountPermission.setPermissionId(id);
            accountPermissionServices.insertSelective(accountPermission);
        }
    }

    @Transactional
    public void insertSelective(Account account, List<String> permissionIdList, ExpertBase expertBase) throws Exception {
        expertBaseServices.insertSelective(expertBase);
        account.setExpertId(expertBase.getId());
        insertSelective(account, permissionIdList);
    }

    @Transactional
    public void insertSelective(Account account, List<String> permissionIdList, StaffInfo staffInfo) throws Exception {
        staffInfoServices.insertSelective(staffInfo);
        account.setStaffId(staffInfo.getId());
        insertSelective(account, permissionIdList);

    }

    @Transactional
    public void insertSelective(Account account, List<String> permissionIdList, CompanyInfo companyInfo) throws Exception {
        companyInfoServices.insertSelective(companyInfo);
        account.setCompanyId(companyInfo.getId());
        insertSelective(account, permissionIdList);
    }

    @Transactional
    public void insertSelective(Account account, String roleId, CompanyInfo companyInfo) throws Exception {
        List<PermissionRole> permissionRoleList = permissionRoleServices.selectByRoleId(roleId);
        List<String> permissionIdList =new ArrayList<String>();
        for (PermissionRole permissionRole : permissionRoleList) {
            permissionIdList.add(permissionRole.getPermissionId());
        }
        account.setRoleId(roleId);
        insertSelective(account,permissionIdList,companyInfo);
    }

    public void updateByPrimaryKeySelective(Account account, List<String> permissionIdList) throws Exception {
        getCurrentProxy().updateByPrimaryKeySelective(account);
        AccountPermissionExample accountPermissionExample = new AccountPermissionExample();
        accountPermissionExample.createCriteria().andAccountIdEqualTo(account.getId());
        List<AccountPermission> accountPermissionList = accountPermissionServices.selectByExample(accountPermissionExample);
        for (int i = 0; i < accountPermissionList.size(); i++) {//去除相同部分
            AccountPermission accountPermission = accountPermissionList.get(i);
            for (int j = 0; j < permissionIdList.size(); j++) {
                if (accountPermission.getPermissionId().equals(permissionIdList.get(j))) {
                    permissionIdList.remove(j);
                    accountPermissionList.remove(i);
                    i--;
                    break;
                }
            }
        }


        for (AccountPermission accountPermission : accountPermissionList) {//删除
            accountPermissionServices.deleteByPrimaryKey(accountPermission.getId());
        }

        AccountPermission accountPermission = new AccountPermission();
        accountPermission.setAccountId(account.getId());
        accountPermission.setType((short) 1);

        for (String id : permissionIdList) {//添加
            accountPermission.setId(IDFactory.getSnowflakeId());
            accountPermission.setPermissionId(id);
            accountPermissionServices.insertSelective(accountPermission);
        }

    }

    public void lazyLoadRole(List<? extends Account> accountList) throws Exception {
        Map<String, Role> map = new HashMap<String, Role>();
        for (Account account : accountList) {
            if (!map.containsKey(account.getRoleId())) {
                map.put(account.getRoleId(), roleServices.selectByPrimaryKey(account.getRoleId()));
            }
            account.setRole(map.get(account.getRoleId()));
        }
    }

    public OffsetLimitPage selectToMainShow(String name, String loginName, String roleId, Integer limit, Integer offset) throws Exception {
        PageHelper.offsetPage(offset, limit);
        List<Account1> accountList = accountMapper.selectToMainShow(name, loginName, roleId);

        lazyLoadRole(accountList);

        OffsetLimitPage offsetLimitPage = new OffsetLimitPage();
        offsetLimitPage.setRows(accountList);
        offsetLimitPage.setTotal(((Page<Account1>) accountList).getTotal());
        return offsetLimitPage;
    }


    public Account selectByLoginName(String loginName) throws Exception {
        AccountExample accountExample = new AccountExample();
        accountExample.createCriteria().andLoginNameEqualTo(loginName);
        List<Account> accountList = selectByExample(accountExample);
        if (accountList.isEmpty()) {
            return null;
        } else {
            return accountList.get(0);
        }
    }


}
